<?php
class PhpGird extends CWidget{
	#表格的ID
	public $id = 'PhpGird-table';
	#Gird的标题
	public $titles ;
	#表格的数据
	public $datas;
	#最记录数
	public $totalNums;
	#当前页码
	public $page;
	#表格记录的主键
	public $contentPK ;
	#删除功能
	public $delUrl ;
	#修改功能
	public $editUrl;
	#新建功能
	public $new = true;
	
	public function run(){
		if ( !is_array( $this->datas ) || !is_array( $this->titles ) ){
			throw new CHttpException(500,'无效要表格参数！');
		}
		echo sprintf( '<div id="%s">' , $this->id );
		#开始输出表格标题
		echo '<table id="table_net" cellspacing="0">
		  <caption>
		  </caption>
		  <tbody><tr>';
		foreach( $this->titles as $value ){
			echo '<th class="Dual 1.8" scope="col">' , $value , '</th>';
		}
		echo '<th class="Dual 1.8" scope="col">操作</th>';
		echo '</tr>';
		#开始输出表格正文
		foreach ( $this->datas as $value ){
			echo '<tr>';
			foreach ( $value as $v ){
				echo '<th class="spec">' , $v , '</th>';
			}
			echo '<th class="spec action">';
			if ( !empty( $this->editUrl ) && !empty( $this->contentPK ) ){
				$tmp_url = Yii::app()->controller->createUrl( $this->editUrl , array( $this->contentPK => $value[ $this->contentPK ] ) );
				echo '<a href="' , $tmp_url , '"><img src="/images/admin/edit.gif" alt="编辑" title="编辑"/></a>';
				
			}
			if ( !empty( $this->delUrl ) && !empty( $this->contentPK ) ){
				$tmp_url = Yii::app()->controller->createUrl( $this->delUrl , array( $this->contentPK => $value[ $this->contentPK ] ) );
				echo '<a href="' , $tmp_url , '" onclick="return confirmDel();"><img src="/images/admin/delete.gif" alt="删除" title="删除"/></a>';
			}
			echo '</th>';
		    echo '</tr>';
		}
		echo '</tbody>';
		echo '</table>';
		$this->addJs();
	}
	
	public function addJs(){
		Yii::app()->clientScript->registerCssFile( '/css/admin_css/table.css' );
		Yii::app()->clientScript->registerScriptFile( Yii::app()->request->hostInfo . Yii::app()->clientScript->getCoreScriptUrl() . '/admin_js/functions.js' , 2 );
	}
}